package lowijs.XfireClient.packet;

import lowijs.XfireClient.*;
import lowijs.XfireClient.event.*;
import lowijs.XfireClient.util.Util;

public class FriendGamePacket extends Packet {
	public FriendGamePacket(byte[] data) {
		int numSids = fromLowHigh(data[9], data[10]);
		Friend[] friends = new Friend[numSids];
		int pos = 11;

		FriendManager fm = FriendManager.getInstance();

		for (int i = 0; i < numSids; i++) {
			byte[] sid = new byte[16];
			System.arraycopy(data, pos, sid, 0, 16);
			friends[i] = fm.getFriendBySid(sid);
			pos += 16;
		}

		pos += 11; // jump to the gameids

		for (int i = 0; i < numSids; i++) {
			int gameid = Util.intFromBytes(data, pos, 4);
			if (friends[i] != null) {
				if (gameid == 0)
					friends[i].setOutOfGame();
				else {
					getGamedataById(gameid);
					friends[i].setInGame(gamename, image);
				}
				EventManager.fireEvent(new FriendGameEvent(friends[i]));
			}
			pos += 4;
		}
	}
}
